|
1
|
|
|
import React from "react"; |
|
2
|
|
|
import { useState, useEffect } from "react"; |
|
3
|
|
|
import scooter from "../models/scooters"; |
|
4
|
|
|
import cities from "../models/cities"; |
|
5
|
|
|
import { ScooterList, Filterbar, RegisterScooterForm } from "../components"; |
|
6
|
|
|
|
|
7
|
|
|
const Scooters = () => { |
|
8
|
|
|
const [filterPhrase, setFilterPhrase] = useState(""); |
|
9
|
|
|
const [displayForm, setDisplayForm] = useState(false); |
|
10
|
|
|
const [scooterData, setScooterData] = useState(); |
|
11
|
|
|
const [cityNames, setCityNames] = useState(); |
|
12
|
|
|
useEffect(() => { |
|
13
|
|
|
fetchScooterData(); |
|
14
|
|
|
}, []); |
|
15
|
|
|
|
|
16
|
|
|
async function fetchScooterData() { |
|
17
|
|
|
const res = await scooter.getScooters(); |
|
18
|
|
|
const data = res.scooters; |
|
19
|
|
|
setScooterData(data); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
useEffect(() => { |
|
23
|
|
|
async function fetchData() { |
|
24
|
|
|
const res = await cities.getCitiesOverview(); |
|
25
|
|
|
const data = res.arrayOverview; |
|
26
|
|
|
let cityNames = []; |
|
27
|
|
|
data.forEach((e) => { |
|
28
|
|
|
cityNames.push(e.name); |
|
29
|
|
|
}); |
|
30
|
|
|
setCityNames(cityNames); |
|
31
|
|
|
} |
|
32
|
|
|
fetchData(); |
|
33
|
|
|
}, []); |
|
34
|
|
|
|
|
35
|
|
|
const handleRegister = async (newScooter) => { |
|
36
|
|
|
//e.preventDefault(); |
|
37
|
|
|
await scooter.addScooter(newScooter); |
|
38
|
|
|
await fetchScooterData(); |
|
39
|
|
|
}; |
|
40
|
|
|
|
|
41
|
|
|
const handleForm = () => { |
|
42
|
|
|
//event.preventDefault(); |
|
43
|
|
|
if (displayForm) { |
|
44
|
|
|
setDisplayForm(false); |
|
45
|
|
|
} else { |
|
46
|
|
|
setDisplayForm(true); |
|
47
|
|
|
} |
|
48
|
|
|
}; |
|
49
|
|
|
|
|
50
|
|
|
const overlay = () => { |
|
51
|
|
|
let state = { click: "auto", backdrop: "blur(0px)" }; |
|
52
|
|
|
if (displayForm) { |
|
53
|
|
|
state = { click: "none", backdrop: "blur(4px)" }; |
|
54
|
|
|
} |
|
55
|
|
|
return state; |
|
56
|
|
|
}; |
|
57
|
|
|
|
|
58
|
|
|
return ( |
|
59
|
|
|
<> |
|
60
|
|
|
{displayForm ? ( |
|
61
|
|
|
<div |
|
62
|
|
|
className="fixed top-1/2 left-1/2 z-10 |
|
63
|
|
|
transform -translate-x-1/2 -translate-y-1/2" |
|
64
|
|
|
> |
|
65
|
|
|
<RegisterScooterForm |
|
66
|
|
|
handleForm={handleForm} |
|
67
|
|
|
scooterData={scooterData} |
|
68
|
|
|
setScooterData={setScooterData} |
|
69
|
|
|
cityNames={cityNames} |
|
70
|
|
|
handleRegister={handleRegister} |
|
71
|
|
|
/> |
|
72
|
|
|
</div> |
|
73
|
|
|
) : ( |
|
74
|
|
|
<div></div> |
|
75
|
|
|
)} |
|
76
|
|
|
|
|
77
|
|
|
<div |
|
78
|
|
|
style={{ |
|
79
|
|
|
pointerEvents: overlay().click, |
|
80
|
|
|
filter: overlay().backdrop, |
|
81
|
|
|
}} |
|
82
|
|
|
className="flex flex-col w-full px-11 min-h-screen" |
|
83
|
|
|
> |
|
84
|
|
|
<div className="text-4xl font-semibold p-3"> |
|
85
|
|
|
<h1>Scooters</h1> |
|
86
|
|
|
</div> |
|
87
|
|
|
<div className="flex flex-row justify-between"> |
|
88
|
|
|
<div className="py-5"> |
|
89
|
|
|
<Filterbar |
|
90
|
|
|
filterPhrase={filterPhrase} |
|
91
|
|
|
setFilterPhrase={setFilterPhrase} |
|
92
|
|
|
placeholder={"Filter Scooters"} |
|
93
|
|
|
/> |
|
94
|
|
|
</div> |
|
95
|
|
|
<div> |
|
96
|
|
|
{cityNames ? ( |
|
97
|
|
|
<button |
|
98
|
|
|
onClick={handleForm} |
|
99
|
|
|
className=" |
|
100
|
|
|
py-2 px-3 transition-colors bg-sidebarHover |
|
101
|
|
|
hover:bg-sidebarBlue text-white rounded-full" |
|
102
|
|
|
> |
|
103
|
|
|
Register Scooter |
|
104
|
|
|
</button> |
|
105
|
|
|
) : ( |
|
106
|
|
|
<div>Please create a city first</div> |
|
107
|
|
|
)} |
|
108
|
|
|
</div> |
|
109
|
|
|
</div> |
|
110
|
|
|
<div className="my-6"> |
|
111
|
|
|
<ScooterList filterPhrase={filterPhrase} scooterData={scooterData} /> |
|
112
|
|
|
</div> |
|
113
|
|
|
</div> |
|
114
|
|
|
</> |
|
115
|
|
|
); |
|
116
|
|
|
}; |
|
117
|
|
|
|
|
118
|
|
|
export default Scooters; |
|
119
|
|
|
|